package com.example.thread.demo.alternate.print5;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class AtomticABC {

    private AtomicInteger atomicInteger = new AtomicInteger(0);
    private static final int MAX_SYC_VALUE = 3 * 10;

    private class RunnableA implements Runnable {
        @Override
        public void run() {
            while (atomicInteger.get() < MAX_SYC_VALUE - 1) {
                if (atomicInteger.get() % 3 == 0) {
                    System.out.println("A");
                    atomicInteger.getAndIncrement();
                }
            }
        }
    }

    private class RunnableB implements Runnable {
        @Override
        public void run() {
            while (atomicInteger.get() < MAX_SYC_VALUE) {
                if (atomicInteger.get() % 3 == 1) {
                    System.out.println("B");
                    atomicInteger.getAndIncrement();
                }
            }
        }
    }

    private class RunnableC implements Runnable {
        @Override
        public void run() {
            while (atomicInteger.get() < MAX_SYC_VALUE) {
                if (atomicInteger.get() % 3 == 2) {
                    System.out.println("C");
                    atomicInteger.getAndIncrement();
                }
            }
        }
    }

    public int get() {
        return atomicInteger.get();
    }

    public static void main(String[] args) {
        AtomticABC atomticABC = new AtomticABC();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 3, 0L,
                TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());
        threadPoolExecutor.execute(atomticABC.new RunnableA());
        threadPoolExecutor.execute(atomticABC.new RunnableB());
        threadPoolExecutor.execute(atomticABC.new RunnableC());

        threadPoolExecutor.shutdown();
    }
}
